package org.linlinjava.litemall.db.service.distribution;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.DisMemberInfoMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.util.DateTools;
import org.linlinjava.litemall.db.vo.TeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * @author sxf
 * @date 2018-07-02
 */
@Service
public class DisMemberInfoService {

    @Autowired
    private DisMemberInfoMapper memberMapper;
    @Autowired
    private  DisMemberAmountService amountService;
    @Autowired
    private LitemallUserService userService;


    public String[] getDetaiCanvas(String id) {

        return null;
    }

    public List<DisMemberInfo> querySelective(String username, String userId, String pId, Integer page, Integer limit, String sort) {
        DisMemberInfoExample example = new DisMemberInfoExample();
        DisMemberInfoExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andDisUserNameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(pId)) {
            criteria.andDisModelIdEqualTo(pId);
        }
        criteria.andIsDeleteEqualTo("N");

        PageHelper.startPage(page, limit);
        return memberMapper.selectByExample(example);

    }

    public int countSeletive(String username, String userId, String pId, Integer page, Integer limit, String sort) {
        DisMemberInfoExample example = new DisMemberInfoExample();
        DisMemberInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andDisUserNameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(pId)) {
            criteria.andDisModelIdEqualTo(pId);
        }
        criteria.andIsDeleteEqualTo("N");

        PageHelper.startPage(page, limit);
        return (int)memberMapper.countByExample(example);

    }

    public void update(DisMemberInfo info) {
        memberMapper.updateByPrimaryKeySelective(info);
    }

    public void add(DisMemberInfo info) {
        memberMapper.insertSelective(info);
    }

    public DisMemberInfo getByUserId(Integer userId) {
        DisMemberInfoExample example = new DisMemberInfoExample();
        DisMemberInfoExample.Criteria criteria = example.createCriteria();

        if (userId!=null) {
            criteria.andDisUserIdEqualTo(userId.toString());
        }else{
            return null;
        }
        criteria.andIsDeleteEqualTo("N");
        List<DisMemberInfo> members = memberMapper.selectByExample(example);
        if(members.size()>0){
            return members.get(0);
        }
        return null;
    }

    public Integer countByPid(Integer userId) {
        DisMemberInfoExample example = new DisMemberInfoExample();
        DisMemberInfoExample.Criteria criteria = example.createCriteria();

        if (userId!=null) {
            criteria.andDisModelIdEqualTo(userId.toString());
        }else{
            return null;
        }
        criteria.andIsDeleteEqualTo("N");
        return (int) memberMapper.countByExample(example);
    }

    public List<TeamVo> getTeamList(Integer userId, Integer page) {
        if(page!=0){
            return memberMapper.getTeamList(userId,(page-1)*5);
        }else{
            return memberMapper.getTeamList(userId,null);
        }

    }

    public Integer getTeamCount(Integer userId) {
        return memberMapper.getTeamCount(userId);
    }

    /**
     * 创建会员和会员账户
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void createMember(Integer userId) {
        LitemallUser user = userService.findById(userId);
        DisMemberInfo memberInfo = this.getByUserId(userId);
        String now = DateTools.getCurrentSysData(DateTools.FULLTIME_FORMAT);
        //会员
        if (memberInfo == null) {
            memberInfo = new DisMemberInfo();
            memberInfo.setDisUserId(userId.toString());
            memberInfo.setDisModelId("3");
            memberInfo.setType(DisMemberInfo.TYPE_Y);
            memberInfo.setIsDelete("N");
            memberInfo.setAddTime(now);
            memberInfo.setUpdateTime(now);
            if(user!=null){
                memberInfo.setDisUserName(user.getNickname());
            }
            this.add(memberInfo);
        }else{
            memberInfo.setType(DisMemberInfo.TYPE_Y);
            memberInfo.setUpdateTime(now);
            this.update(memberInfo);
        }
        //创建账户
        DisMemberAmount amount =  amountService.getByuserId(userId);
        if(amount==null){
            amount = new DisMemberAmount();
            amount.setDisUserId(userId.toString());
            amount.setDisUserName(user.getNickname());
            amount.setAddTime(now);
            amount.setUpdateTime(now);
            amountService.insert(amount);
        }
    }
}
